<?php


namespace RedMVC\Form\Validator;

/**
 * Float class
 *  
 * @category    RedMVC
 * @package     Form
 * @subpackage  Validator 
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class Float extends AValidator{
    
    private $precision = null;
    private $decimals = null;
    
    public function __construct($precision, $decimals){
        $this->precision = $precision;
        $this->decimals = $decimals;
    }

    public function isValid($value, $formValues = null){
        /*
        if(!is_numeric($value) || $value !== number_format($value, $this->decimals, '.', '')){
            $this->setError('
                Keine gültige Gleitkommazahl. 
                Bitte . als Trennzeichen und nur '.$this->decimals.' Nachkommastellen.');
            return false;
        }
        return true;
        */
        
        /*
        if(ereg('^[0-9]+\.[0-9]{3,}$', $value)){
            return true;
        }  
        */
   
        $max = (float)str_pad("", $this->precision-$this->decimals, '9').'.'.str_pad("", $this->decimals, '9');
        $min = (float)"-$max";
        
        if(!is_numeric($value) || ($value < $min) || ($value > $max)){
            $this->setError('FORM_VALIDATOR_FLOAT', $max);
            return false;
        }
        return true;  
    }
}